package 力扣._8040_生成特殊数字的最少操作;

public class Solution {
    public int minimumOperations(String num) {
        //25的倍数尾数特点，00,25,50,75
        //分两种一种凑0，一种凑5
        int last_0 = -1;
        int last_5 = -1;

        int last2_0 = -1;   //倒数第二个元素
        int total_0 = 0;
        for (int i = num.length() - 1; i >= 0; i--) {
            if (last_0 > 0 && num.charAt(i) == '0') {    //00
                last2_0 = i;
                return num.length() - last2_0 - 2;
            }
            if (last_0 > 0 && num.charAt(i) == '5') {    //50
                last2_0 = i;
                return num.length() - last2_0 - 2;
            }
            if (last_5 > 0 && num.charAt(i) == '7') {    //75
                last2_0 = i;
                return num.length() - last2_0 - 2;
            }
            if (last_5 > 0 && num.charAt(i) == '2') {    //25
                last2_0 = i;
                return num.length() - last2_0 - 2;
            }
            if (last_0 == -1 && num.charAt(i) == '0') {
                last_0 = i;
            }
            if (last_5 == -1 && num.charAt(i) == '5') {
                last_5 = i;
            }

            if (num.charAt(i) == '0')
                total_0++;
        }
        return num.length() - total_0;
    }

    public static void main(String[] args) {
        int i = new Solution().minimumOperations("2245047");
        System.out.println(i);
    }
}
